﻿package com.eaglesandcobras.common.sounds {
	import flash.media.Sound;
	import flash.net.URLRequest;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.events.Event;
	import flash.utils.getDefinitionByName;
	import com.eaglesandcobras.common.events.GameEvent;
	import flash.events.EventDispatcher;
	
	public class SoundLoader extends EventDispatcher{
		private var _sounds:Object;
		private var _sound:Sound;
		private var _volume:Number = .5;
		private var _transform:SoundTransform;
		private var _ltransform:SoundTransform;
		private var _muteTransform:SoundTransform;
		private var _efx:SoundChannel;
		private var _loop:SoundChannel;
		private var _lp:String;
		private var _soundPlay:Boolean = true;
		private var _sc:Object;
		private var _mute:Boolean;
		
		/*************************
		CONSTRUCTOR
		*************************/
		public function SoundLoader() {
			super();
			init();
		}
		
		private function init(){
			_efx = new SoundChannel();
			
			_sounds = new Object();
			
			_ltransform = new SoundTransform();
			_ltransform.volume = .2;
			
			_transform = new SoundTransform();
			_transform.volume = .5;
			
			_muteTransform = new SoundTransform();
			_muteTransform.volume = 0;
		}		
		
		/*************************
		LOAD INTERNAL
		*************************/
		public function loadSounds():void {
			trace ("override loadSounds. I need Sounds");
		}
		
		/*************************
		ADD A SOUND
		*************************/
		public function addSound($soundPath:String, $external:Boolean=true):void {
			if ($external) loadExternal($soundPath);
			else loadInternal($soundPath);
		}
		
		private function loadExternal($soundPath:String):void {
			_sound = new Sound();
			_sound.load(new URLRequest($soundPath));
			
			_sounds[$soundPath] = _sound;
		}
		
		private function loadInternal($soundPath:String):void {
			var _class:Class = getDefinitionByName($soundPath) as Class;
			var _sound = new _class() as Sound;
			
			_sounds[$soundPath] = _sound;
		}
		
		/**********************************
		PLAY SOUND
		***********************************/
		public function playSound($soundPath:String):void {
			if (_soundPlay) {
				_efx = _sounds[$soundPath].play();
				_efx.soundTransform = _transform;
			}
		}
		
		/**********************************
		GLOBAL SOUND CONTROL
		***********************************/
		public function switchSound():Boolean{
			trace (_mute);
			if (_soundPlay) {
				_soundPlay=false;
				if (_loop) _loop.stop();
			}else {
				_soundPlay = true;
				if (_loop) replayLoop();
			}
			
			this.dispatchEvent( new GameEvent (GameEvent.GLOBAL_SOUND) );
			return _soundPlay;
		}
		
		public function get soundState():Boolean {
			return _soundPlay;
		}
		
		/**********************************
		LOOP SOUND
		***********************************/
		public function loopSound($soundPath:String):void {
			_lp = $soundPath;
			if (_soundPlay) {
				_loop = _sounds[$soundPath].play();
				_loop.soundTransform = _ltransform;
				_loop.addEventListener(Event.SOUND_COMPLETE, replayLoop);
			}
		}
		
		private function replayLoop(e:Event=null):void{
			_loop.removeEventListener(Event.SOUND_COMPLETE, replayLoop);
			
			if (_soundPlay) {
				_loop = _sounds[_lp].play();
				if (!_mute) _loop.soundTransform = _ltransform;
				else _loop.soundTransform = _muteTransform;
				_loop.addEventListener(Event.SOUND_COMPLETE, replayLoop);
			}
		}
		
		public function mute($b:Boolean):void{
			_mute = $b;
			
			if (_mute) {
				_loop.soundTransform = _ltransform;
				_mute = false
			} else {
				_loop.soundTransform = _muteTransform;
				_mute=true;
			}
		}
	}
}